//
//  SLEditImageController.m
//  DarkMode
//
//  Created by wsl on 2019/10/31.
//  Copyright © 2019 wsl. All rights reserved.
//

#import "SLEditImageController.h"
#import <Photos/Photos.h>
#import "UIView+SLImage.h"
#import "SLBlurView.h"
#import "SLEditMenuView.h"
#import "SLEditSelectedBox.h"
#import "SLImage.h"
#import "SLImageView.h"
#import "SLDrawView.h"
#import "SLEditTextView.h"
#import "SLMosaicView.h"
#import "UIImage+SLCommon.h"
#import "SLImageZoomView.h"
#import "SLImageClipController.h"
#import "AssetsLibrary/AssetsLibrary.h"

@interface SLEditImageController ()<UIGestureRecognizerDelegate, SLImageZoomViewDelegate>

@property (nonatomic, strong) SLImageZoomView *zoomView; // 预览视图 展示编辑的图片 可以缩放

@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, strong) UIButton *saveAlbumBtn;

@property (nonatomic,strong)UIImageView *imgView;

@end

@implementation SLEditImageController

#pragma mark - Override
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (BOOL)prefersStatusBarHidden {
    return YES;
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"图片编辑视图释放了");
}

#pragma mark - UI
- (void)setupUI {
    [self.view addSubview:self.zoomView];
    self.zoomView.pinchGestureRecognizer.enabled = NO;
    self.zoomView.image = self.image;

    [self.view addSubview:self.saveAlbumBtn];
    
    [self.view addSubview:self.cancelBtn];
    
}



//置顶视图
- (void)topSelectedView:(UIView *)topView {
    [self.zoomView.imageView bringSubviewToFront:topView];
    [SLDelayPerform sl_cancelDelayPerform]; //取消延迟执行
}
// 隐藏预览按钮
- (void)hiddenPreviewButton:(BOOL)isHidden {
    self.saveAlbumBtn.hidden = isHidden;
}

#pragma mark - Getter

- (SLImageZoomView *)zoomView {
    if (_zoomView == nil) {
        _zoomView = [[SLImageZoomView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
        _zoomView.backgroundColor = UIColor.blackColor;
        _zoomView.userInteractionEnabled = YES;
        _zoomView.maximumZoomScale = 2;
        _zoomView.zoomViewDelegate = self;
    }
    return _zoomView;
}
- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(16, 56, 30, 30)];
        [_cancelBtn setImage:[UIImage imageNamed:@"zl_retake"] forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(againShotBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIButton *)saveAlbumBtn {
    if (_saveAlbumBtn == nil) {
        _saveAlbumBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
        _saveAlbumBtn.center = CGPointMake(self.view.sl_width/2.0 + 70/2.0+ (self.view.sl_width/2 - 70/2.0)/2.0, self.view.sl_height - 80);
        
//        [_saveAlbumBtn setImage:[UIImage imageNamed:@"social_confirm_tag"] forState:UIControlStateNormal];
        [_saveAlbumBtn setBackgroundImage:[UIImage imageNamed:@"social_confirm_tag"] forState:UIControlStateNormal];
        
        [_saveAlbumBtn addTarget:self action:@selector(saveAlbumBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [_saveAlbumBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_saveAlbumBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _saveAlbumBtn.titleLabel.font = font(16);
    }
    return _saveAlbumBtn;
}

#pragma mark - Events Handle
//再试一次 继续拍摄
- (void)againShotBtnClicked:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}
//保存到相册
- (void)saveAlbumBtnClicked:(id)sender {
    if(self.image) {
        [self dismissViewControllerAnimated:NO completion:nil];
        UIViewController *parentVC = self.presentingViewController;
        UIViewController *bottomVC;
        while (parentVC) {
            bottomVC = parentVC;
            parentVC = parentVC.presentingViewController;
        }
        [bottomVC dismissViewControllerAnimated:NO completion:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"updateReleasePhoto" object:nil userInfo:@{@"data":self.image}];
    }
}
//保存图片完成后调用的方法
- (void)savedPhotoImage:(UIImage*)image didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo {

    
    SL_DISPATCH_ON_MAIN_THREAD(^{

    });
    if (error) {
        NSLog(@"保存图片出错%@", error.localizedDescription);
    } else {
        NSLog(@"保存图片成功");
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"updateReleaseSocialList" object:nil];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"updateReleasePhoto" object:nil userInfo:@{@"data":image}];
    }
}



#pragma mark - UIGestureRecognizerDelegate
// 该方法返回的BOOL值决定了view是否能够同时响应多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    //     NSLog(@"%@ - %@", gestureRecognizer.class, otherGestureRecognizer.class);
    return YES;
}

#pragma mark - SLZoomViewDelegate
- (void)zoomViewDidEndMoveImage:(SLImageZoomView *)zoomView {
   
}

@end
